/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

/**
 *
 */
package com.wallellen.wechat.token.timer;

import com.wallellen.wechat.token.Ticket;
import com.wallellen.wechat.token.TicketType;
import com.wallellen.wechat.token.server.CustomerServer;
import com.wallellen.wechat.token.server.JsApiTicketServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.TimerTask;

/**
 * @author <a href="mailto:wallellen@hotmail.com">WALLE</a>
 * @date 2015年1月29日
 */
public class JsApiTicketTimer extends TimerTask {

    private static Logger logger = LoggerFactory.getLogger(JsApiTicketTimer.class);
    // jsapi_ticket有效期7200秒,提前200秒请求新的token
    public static final long PERIOD = 7000 * 1000;
    public static final long DELAY = 0; // 此任务的延迟时间为0，即立即执行

    @Override
    public void run() {
        logger.info("jsapi_ticket 定时任务启动，获取新的jsapi_ticket");
        // 得到新的access token
        Ticket jsapiTicket = new Ticket(TicketType.jsapi);
        // 手动获取成功之后持久化accessToken
        if (jsapiTicket.request()) {
            JsApiTicketServer jsapiTicketServer = new JsApiTicketServer();
            CustomerServer customerServer = (CustomerServer) jsapiTicketServer
                    .customerServer();
            customerServer.save(jsapiTicket);
        }
    }

}
